/*************************************************
Author: will
Date: 10/24/2012
Description:
Notes:
*************************************************/

#ifndef __CScrollConfig_h__
#define __CScrollConfig_h__

#include "CTArray.h"
#include "commclass.h"
#include "commerrno.h"
#include "CDropItemSubType.h"

class CScrollMaterial
{
public:
	CScrollMaterial ()
	{
		_usMaterialId = 0;
		_usMaterialCnt = 0;
	}

	unsigned short _usMaterialId;
	unsigned short _usMaterialCnt;
};

class CScrollConfigData
{
public:
	CScrollConfigData()
        :_bHasConfig(false)
    {
	}

	virtual ~CScrollConfigData()
    {
	}

public:
    bool _bHasConfig;

	unsigned short _usSrollConfigID;
    unsigned short _usUseLevel;
    CTSizeString<DROP_ITEM_CFG_SUBTYPE_STR_LEN> _szDropItemSubType;

    unsigned int   _uiSilverBuyPrice;
    unsigned int   _uiSilverSellPrice;
    unsigned int   _uiContriPrice;
    unsigned int   _uiGoldBuyPrice;

	CTArray<CScrollMaterial, MAX_SCROLL_MATERIAL_NUM> _astMaterial;

    unsigned int       _uiNeedGold;  //合成花费的金币数
    unsigned short     _usOldEquipConfigID;
    unsigned char      _ucNewItemType;
    unsigned short     _usNewEquipConfigID;
    CDropInstanceConfig _stDropInstace;

    bool               _bCanAutoSellInIns;  //在副本中是否可以自动出售
};

class CScrollConfig
{
public:
    CScrollConfig()
    {

    }

    virtual ~CScrollConfig()
    {
    }

    static size_t CountSize()
    {
        return CDropItemSubTypeConfig::CountSize(MAX_SCROLL_CONFIG_COUNT);
    }

    int Init(const char* sBuff)
    {
        return _stDropItemSubTypeConfig.Init(sBuff, MAX_SCROLL_CONFIG_COUNT);
    }

    int AddConfig(CScrollConfigData& stConfigData)
    {
        int iRet = 0;

        if (0 == stConfigData._usSrollConfigID
            || stConfigData._usSrollConfigID > MAX_SCROLL_CONFIG_COUNT)
        {
            CT_ERROR(("error scroll config %d", stConfigData._usSrollConfigID));
            return RET_SYSTEM_FAIL;
        }

        if (_astScrollConfigDatas[stConfigData._usSrollConfigID]._bHasConfig)
        {
            CT_ERROR(("duplicate scroll config %d", stConfigData._usSrollConfigID));
            return RET_SYSTEM_FAIL;
        }

        _astScrollConfigDatas[stConfigData._usSrollConfigID] = stConfigData;
        _astScrollConfigDatas[stConfigData._usSrollConfigID]._bHasConfig = true;

        //添加掉落类型配置
        if (stConfigData._szDropItemSubType.GetDataLen() > 0)
        {
            CDropItemSubTypeConfigData stItemDropSubTypeData;
            stItemDropSubTypeData._iItemID = stConfigData._usSrollConfigID;
            iRet = _stDropItemSubTypeConfig.AddSubType(stConfigData._szDropItemSubType, stItemDropSubTypeData);
            CHECK_RETURN(iRet);
        }

        return 0;
    }

    bool IsScrollValid(unsigned short usScrollConfigID)
    {
        if (0 == usScrollConfigID
            || usScrollConfigID > MAX_SCROLL_CONFIG_COUNT)
        {
            return false;
        }

        if (!_astScrollConfigDatas[usScrollConfigID]._bHasConfig)
        {
            return false;
        }

        return true;
    }

    int GetConfig(unsigned short usScrollConfigID, CScrollConfigData& stConfigData)
    {
        if (!IsScrollValid(usScrollConfigID))
        {
            return ERROR_ITEM_NO_SCROLL_CONFIG;
        }

        stConfigData = _astScrollConfigDatas[usScrollConfigID];

        return 0;
    }

    int GetConfig(CDropItemSubTypeConfigData::T_DROP_ITEM_SUBTYPE_CONFIG_KEY& szKey,
                        CScrollConfigData& stConfigData)
    {
        int iRet = 0;

        CDropItemSubTypeConfigData stItemDropSubTypeData;
        iRet = _stDropItemSubTypeConfig.GetRandomData(szKey, stItemDropSubTypeData);
        CHECK_RETURN(iRet);

        unsigned short usScrollConfigID = (unsigned short)stItemDropSubTypeData._iItemID;
        return GetConfig(usScrollConfigID, stConfigData);
    }

public:
    CTLib::CTArray<CScrollConfigData, MAX_SCROLL_CONFIG_COUNT + 1> _astScrollConfigDatas;
    CDropItemSubTypeConfig _stDropItemSubTypeConfig;
};



#endif // __CScrollConfig_h__

